using System;
using System.Collections;
using System.Collections.Generic;
using ProjectSurviver;
using UnityEngine;

public class AchievementItem
{
    public string Key { get; private set; }    //用来存储的Key
    public string Name { get; private set; }    //用来显示的名字
    public string Description { get; private set; } //描述
    public bool Unlocked { get; set; }    //判断是否解锁
    public string IconName { get; private set; }    //图标名字
    public Func<bool> mCondition;    //判断是否解锁的方法
    public Action<AchievementItem> mOnUnlocked;    //解锁的方法

    public AchievementItem WithKey(string key)    //链式的封装
    {
        Key = key;
        return this;
    }

    public AchievementItem WithName(string name)
    {
        Name = name;
        return this;
    }

    public AchievementItem WithIconName(string iconName)
    {   
        IconName = iconName;
        return this;
    }

    public AchievementItem WithDescription(string description)
    {
        Description = description;
        return this;
    }

    public AchievementItem Condition(Func<bool> condition)    //判断是否解锁的方法
    {
        mCondition = condition;
        return this;
    }

    public AchievementItem OnUnlocked(Action<AchievementItem> onUnlocked)    //解锁的方法
    {
        mOnUnlocked = onUnlocked;
        return this;
    }

    public bool ConditionCheck()    //判断是否对外显示的方法
    {
       return mCondition(); 
    }

    public AchievementItem Load(SaveSystem saveSystem)    //加载的方法
    {
        Unlocked = saveSystem.LoadBool($"achievement_first_{Key}");    //从本地加载
        return this; 
    }

    public void Unlock(SaveSystem saveSystem)    //解锁的方法
    {
       Unlocked = true;
       saveSystem.SaveBool($"achievement_first_{Key}", true);    //保存到本地
       mOnUnlocked?.Invoke(this);    //触发解锁的方法
       AchievementSystem.OnAchievementUnlocked.Trigger(this);    //触发OnAchievementSystemChange的方法
    }
}
